<?php
/*
 * @Author: your name
 * @Date: 2020-06-06 09:13:05
 * @LastEditTime: 2020-06-06 21:14:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hotel\application\api\controller\Order.php
 */
namespace app\api\controller;

use think\Controller;
use think\Db;

class Second extends Base
{

    public function secondlist()
    {
        $user_id = $this->user_id;
        $time = time();
        $list = db('second_card_user')
            ->alias('u')
            ->join('second_card c','c.id=u.card_id')
            ->where(['u.user_id'=>$user_id,'u.card_num'=>['>','0'],'u.endtime'=>['>',$time]])
            ->field(['u.id'=>'id','c.title'=>'title','u.endtime'=>'endtime','c.logo'=>'logo','u.card_num'=>'card_num'])
            ->order('id desc')
            ->select();
            foreach ($list as $i => $item) {
                $list[$i]['endtime'] = date('Y-m-d',$item['endtime']);
            }
           
        return model('Res')->ctx($list);
    }
    public function secondorderlist()
    {
        $type = input('type');
        $page = input('page');
        $offset = ($page-1)*10;
        $user_id = $this->user_id;
        $where = array();
        if($type!=0){
            $where['o.status'] = $type-1;
        }
        $where['o.user_id'] = $user_id;
        $where['is_delete'] = 0;
        $time = time();
        $list = db('second_card_order')
            ->alias('o')
            ->join('second_card s','o.card_id = s.id')
            ->where($where)
            ->limit($offset, 10)
            ->order('id desc')
            ->field('o.id as id,o.recharge_number as recharge_number,o.addtime as addtime,num,o.status as status,s.endtime as endtime,o.money as money,logo,title')
            ->select();
         
        foreach($list as $k=>&$v){
            if($v['endtime']==0){
                $v['endtime'] = "无期限";
            }else{
                $endtime = $v['addtime']+$v['endtime']*24*60*60;
                $v['endtime'] = date('Y-m-d',$endtime);
            }
            $v['addtime'] = date('Y-m-d',$v['addtime']);
        } 
         
        $list = model('Res')->ctx($list);
        if(isset($list['data'])){
            return $list;
        }else{
            $list['data'] = [];
            return $list;
        }
    }


     

}
